<%-- 
    Document   : allStops
    Created on : Jul 28, 2009, 12:58:46 PM
    Author     : david
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"
        import="com.appspot.nnaplanner.*,
        java.util.List,
        java.util.Set,
        com.google.appengine.api.datastore.Query,
        com.google.appengine.api.datastore.DatastoreServiceFactory,
        com.appspot.nnaplanner.DAL.*,
        java.util.logging.Logger
        "%>
<%

            Query query = new Query(DALStop.class.getSimpleName());
            javax.jdo.Query q = DAL.newQuery();
            int start = 0 + Integer.parseInt(request.getParameter("start"));
            int limit = 0 + Integer.parseInt(request.getParameter("limit"));
            String keyword = "" + request.getParameter("keyword");
            //System.console().
            if (keyword.equals("null")) {
                keyword = "";
            }
            Logger log = Logger.getLogger(this.getServletInfo());
            log.info("limit:" + limit + ", start: " + start + ", keyword:" + keyword);
            if (!keyword.isEmpty()) {
                q.setFilter("_keywords == pKeyword");
                q.declareParameters("String pKeyword");
                keyword = keyword.toLowerCase();
                //keyword = keyword.toLowerCase();
                //query.addFilter("_keywords",Query.FilterOperator.EQUAL, keyword);

            }
            int total = DatastoreServiceFactory.getDatastoreService().prepare(query).countEntities();
            q.setClass(DALStop.class);
            q.setRange(start, start + limit);
            List<DALStop> result = (List<DALStop>) q.execute(keyword);

            out.print("{\"total\": \"" + total + "\", \"stops\":[");
            int count = 0;
            for (DALStop s : result) {
                if (count > 0) {
                    out.print(", ");
                }
                out.print("{");
                out.print("\"stopid\": \"" + s.getID() + "\", ");
                out.print("\"name\": \"" + s.getName() + "\", ");
                out.print("\"description\": \"" + s.getDescription() + "\", ");
                out.print("\"connections\": \"" + s.getConnectionCount() + "\", ");
                out.print("\"latitude\": \"" + s.getLatitude() + "\", ");
                out.print("\"longitude\": \"" + s.getLongitude() + "\"");
                out.print("}");
                count++;
            }
            out.print("] }");
%>